<?php

/**
 * Created by PhpStorm.
 * User: PX780
 * Date: 2016/10/30
 * Time: 21:02
 */
class myimage{
    public static $imageSize;
    public static $mergedImg;
    public static $smallImage;
    public static $font = LIB_PATH.'font/yahei.ttf';//字体路径
    public static $font1 = LIB_PATH.'font/yaheixi.ttf';//细字体路径
    public static $font2 = LIB_PATH.'font/yahei.ttf';//粗字体路径
    public static $fontSize = 22;

    //默认创建方形的图片，在当前图片所在目录下创建一个子目录
    public static function resize($oldImg,$newWidth,$isCut=0,$newImg='',$newHeight=0,$quality=100){
        require_once("resizeimage.php");
        if(!$newHeight) $newHeight = $newWidth;
        if(!$newImg) {
            $pathInfo = pathinfo($oldImg);
            $newDir = $pathInfo['dirname']."/$newWidth";
            if(!is_dir($newDir)){
                mkdir($newDir);
                chmod($newDir,0777);
            }

            $newImg = $newDir."/".$pathInfo['basename'];
        }

        $resizeimage=new ResizeImage($oldImg,$newImg,$newWidth,$newHeight,$isCut,$quality);
    }

    public static function init($width,$height){
        $image_3 = imageCreatetruecolor($width,$height);
        $white = imagecolorallocate($image_3, 255, 255, 255);
        $black = imagecolorallocate($image_3, 0x00, 0x00, 0x00);//字体颜色

        // 为真彩画布创建白色背景
        imagefill($image_3, 0, 0, $white);
        //imageColorTransparent($image_3, $white);// 设置透明
        self::setMergedImg($image_3);
    }

    public static function setFontSize($size){
        self::$fontSize = $size;
    }

    public static function setFont($fontFilePath){
        self::$font = $fontFilePath;
    }

    public static function setSmallFont(){
        self::$font = self::$font1;
    }

    public static function setBoldFont(){
        self::$font = self::$font2;
    }

    public static function setMergedImg($mergedImg){
        self::$mergedImg = $mergedImg;
    }

    public static function showJpeg(){
        header('Content-Type: image/jpg');
        imagejpeg(self::$mergedImg);
    }

    public static function clear(){
        imagedestroy(self::$mergedImg);
        if(self::$smallImage) imagedestroy(self::$smallImage);
    }

    public static function writeText($txt,$startX,$startY,$red=0,$green=0,$blue=0){
        $black = imagecolorallocate(self::$mergedImg, $red, $green, $blue);//字体颜色

        imagefttext(self::$mergedImg, self::$fontSize, 0, $startX, $startY + self::$fontSize/2, $black, self::$font,$txt);
    }

    public static function copyToCenter(&$smallImage,$newSmallHeight,$marginTop=0){
        return self::mergeToCenter($smallImage,$newSmallHeight,$marginTop);
    }

    /*
     * 把第一个图片，复制到第二个图片的上边
     * */
    public static function mergeToCenter(&$smallImage,$newSmallHeight,$marginTop=0)
    {
        self::$smallImage = $smallImage;
        $srcWidth = imagesx($smallImage);
        $srcHeight = imagesy($smallImage);

        $dstWidth = imagesx(self::$mergedImg);

        $rate = $newSmallHeight / $srcHeight;
        $newWidth = $srcWidth * $rate;

        //只显示中间部分的图片
        if ($newWidth > $dstWidth){
            $dstX = 0;
            $srcX = ($newWidth - $dstWidth) / 2;
        }else{
            $srcX = 0;
            $dstX = ($dstWidth - $newWidth) / 2;
        }

        imagecopyresampled(self::$mergedImg, $smallImage, $dstX, $marginTop, $srcX, 0,$newWidth, $newSmallHeight, $srcWidth, $srcHeight);
        if(self::$smallImage) imagedestroy(self::$smallImage);
    }

    public static function copyToPos(&$smallImage,$dstX,$desY){
        return self::mergeToPos($smallImage,$dstX,$desY);
    }

    /*
         * 把第一个图片，复制到第二个图片的上边
         * */
    public static function mergeToPos(&$smallImage,$dstX,$desY)
    {
        self::$smallImage = $smallImage;
        $srcWidth = imagesx($smallImage);
        $srcHeight = imagesy($smallImage);

        imagecopyresampled(self::$mergedImg, $smallImage, $dstX, $desY, 0, 0,$srcWidth, $srcHeight, $srcWidth, $srcHeight);
        if(self::$smallImage) imagedestroy(self::$smallImage);
    }


    public static function getImageType ($filename){
        $imageTypeArray = array//无用，仅仅示意
        (
            0=>'UNKNOWN',
            1=>'GIF',
            2=>'JPEG',
            3=>'PNG',
            4=>'SWF',
            5=>'PSD',
            6=>'BMP',
            7=>'TIFF_II',
            8=>'TIFF_MM',
            9=>'JPC',
            10=>'JP2',
            11=>'JPX',
            12=>'JB2',
            13=>'SWC',
            14=>'IFF',
            15=>'WBMP',
            16=>'XBM',
            17=>'ICO',
            18=>'COUNT'
        );

        $size = getimagesize($filename);
        return $size[2];
        //$size[2] = $imageTypeArray[$size[2]];
    }

    public static function getImageSize($filename){
        return getimagesize($filename);
    }

    public static function  createFromAny($filePath) {
        self::$imageSize = self::getImageSize($filePath); //
        $allowedTypes = array(
            1,  // [] gif
            2,  // [] jpg
            3,  // [] png
            6   // [] bmp
        );
        if (!in_array(self::$imageSize[2], $allowedTypes)) {
            return false;
        }
        switch (self::$imageSize[2]) {
            case 1 :
                $im = imageCreateFromGif($filePath);
                break;
            case 2 :
                $im = imageCreateFromJpeg($filePath);
                break;
            case 3 :
                $im = imageCreateFromPng($filePath);
                break;
            case 6 :
                $im = imageCreateFromBmp($filePath);
                break;
        }
        return $im;
    }


    public static function mergePngAndJpg($pngFile,$jpgFile,$targetX,$targetY){
        // 创建图片对象
        $image_1 = imagecreatefrompng($pngFile);
        $image_2 = imagecreatefromjpeg($jpgFile);
        // 创建真彩画布
        $image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1));
        // 为真彩画布创建白色背景
        $color = imagecolorallocate($image_3, 0, 0, 0);
        imagefill($image_3, 0, 0, $color);
        // 设置透明
        imageColorTransparent($image_3, $color);
        // 复制图片一到真彩画布中（重新取样-获取透明图片）
        imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
        // 与图片二合成
        $srcX = 0;
        $srcY = 0;
        imagecopymerge($image_3, $image_2, $targetX, $targetY, $srcX, $srcY, imagesx($image_2), imagesy($image_2), 100);
        // 输出合成图片
        $up = "up/".uniqid().".png";
        imagepng($image_3, FRONT_ROOT."web/$up");

        return $up;
    }


}
